//  https://www.luogu.com.cn/problem/P1775

#include<iostream>
#include<cstring>
using namespace std;

const int N = 1010;
int sum[N];
int dp[N][N];

int main()
{
    int n;cin >> n;
    for (int i = 1;i <= n;i++)
    {
        int x;cin >> x;
        sum[i] = sum[i - 1] + x;
    }

    memset(dp, 0x3f, sizeof dp);
    for (int i = 0;i <= n;i++) dp[i][i] = 0;

    for (int len = 1;len <= n;len++)
    {
        for (int i = 1;i + len - 1 <= n;i++)
        {
            int j = i + len - 1;
            int t = sum[j] - sum[i - 1];
            for (int k = i;k < j;k++)
            {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + t);
            }
        }
    }
    cout << dp[1][n];
    return 0;
}